// hyb_ylmz/personal_page/detail/patient_detail/patient_detail.js
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    disabled: false,
    img_arr: [],
    data_arr: [],
  },
  chakanimg(e) {
    var that = this;
    wx.previewImage({
      current: e.currentTarget.dataset.src,
      urls: that.data.info.pic,
    })
  },
  upload: function() {
    var that = this
    var uniacid = that.data.uniacid
    var data_arr = that.data.data_arr;
    for (var i = 0; i < this.data.img_arr.length; i++) {
      wx.uploadFile({
        url: that.data.url + 'app/index.php?i=' + uniacid + '&c=entry&a=wxapp&do=Upload&m=hyb_yl',
        filePath: that.data.img_arr[i],
        name: 'upfile',
        formData: [],
        success: function(res) {
          console.log(res)
          data_arr.push(res.data)
          that.setData({
            data_arr: data_arr
          })
        }
      })
    }
    this.setData({
      formdata: ''
    })
  },
  upimg: function() {
    var that = this;
    console.log(this.data.img_arr)
    if (this.data.img_arr.length < 3) {
      wx.chooseMessageFile({
        count: 3,
        type: 'all',
        success: function(res) {
          console.log(res)
          if (res.tempFiles.length == 3) {
            that.setData({
              hide: true
            })
          }
          that.setData({
            img_arr: that.data.img_arr.concat(res.tempFiles[0].path),
            name: res.tempFiles[0].name,
            ppt:'https://lg-o8nytxik-1257013711.cos.ap-shanghai.myqcloud.com/ppt.png'
          })
        }
      })
    } else {
      wx.showToast({
        title: '最多上传三张图片',
        icon: 'loading',
        duration: 3000
      });
    }
  },
  formSubmit(e) {
    var that = this
    var values = e.detail.value
    var dmoney = values.dmoney //处方价格
    var content = values.rg //处方内容
    var userid = values.hzid //患者ID
    var docid = values.zid //专家ID
    var username = values.username //患者名字
    var ky_yibao = values.dorder //订单号
    var docname = values.docname
    var cfzhenj = values.cfzhenj
    var data_arr = that.data.data_arr;
    var useropenid = that.data.useropenid
    this.upload();
    wx.showToast({
      title: '上传中，请稍后',
      icon: "loading"
    })
    setTimeout(function() {
      if (e.detail.value.rg == "") {
        wx.showToast({
          title: '请填写处方',
          image: "/hyb_yl/images/err.png"
        })
      } else {
        app.util.request({
          'url': 'entry/wxapp/Saverecipe',
          data: {
            userid: userid,
            content: content,
            docid: docid,
            orderarr: ky_yibao,
            username: values.username,
            pic: data_arr,
            useropenid: useropenid,
            docname: docname,
            cfzhenj: cfzhenj

          },
          success: function(res) {
            //打印
            //console.log(res)
            app.util.request({
              'url': 'entry/wxapp/Dyjj',
              data: {
                username: username,
                content: content,
                dmoney: dmoney,
                docname: docname,
                ky_yibao: ky_yibao
              },
              success: function(res) {
                //console.log(res)
              }
            })
            //通知邮箱
            wx.showToast({
              title: '提交成功',
              icon: 'success',
              duration: 2000,
              success: function() {
                wx.navigateBack({
                  delta: 1,
                })
              }
            })
          },
          fail: function(err) {
            console.log(err)
          },

        })

      }
    }, 3000)
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this
    var username = options.username
    var ksname = options.ksname
    var money = options.money
    var phone = options.phone
    var tjtime = options.tjtime
    var yytime = options.yytime
    var dorder = options.dorder
    var sex = options.sex
    var age = options.age
    var zjid = options.zjid
    var hzid = options.hzid
    var bgc = wx.getStorageSync('color')
    var cid = options.cid
    var useropenid = options.useropenid
    var uniacid = app.siteInfo.uniacid;
    var xq = options.xq
    app.util.request({
      'url': 'entry/wxapp/url',
      'cachetime': '0',
      success: function(res) {
        console.log(res)
        that.setData({
          url: res.data
        })
      },
    })
    if (options.utype !== 'undefined') {
      that.setData({
        utype: options.utype
      })
    }
    if (cid) {
      app.util.request({
        'url': 'entry/wxapp/Selcetcfinfo',
        data: {
          cid: cid
        },
        success: function(res) {
          console.log(res)
          that.setData({
            dorder: res.data.data.orderarr,
            username: res.data.data.username,
            sex: res.data.data.mysex,
            age: res.data.data.myage,
            phone: res.data.data.myphone,
            dmoney: res.data.data.dmoney,
            money: res.data.data.money,
            content: res.data.data.content,
            info: res.data.data,
            cfzhenj: res.data.data.cfzhenj
          })
        }
      })
    }
    var z_name = options.z_name
    wx.setNavigationBarColor({
      frontColor: "#ffffff",
      backgroundColor: bgc,
    })
    // 判断当前是否是被确认状态
    var that = this
    var id = options.id
    var ky_yibao = options.ky_yibao
    that.setData({
      username: username,
      ksname: ksname,
      money: money,
      phone: phone,
      tjtime: tjtime,
      yytime: yytime,
      dorder: dorder,
      sex: sex,
      age: age,
      zjid: zjid,
      hzid: hzid,
      z_name: z_name,
      uniacid: uniacid,
      useropenid: useropenid,
      xq: xq
    })
    //订单号查询用户ID
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {
    this.getDocinfo()
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },
  getDocinfo: function() {
    var that = this
    var zid = that.data.zjid
    app.util.request({
      'url': 'entry/wxapp/Docinfo',
      data: {
        zid: zid
      },
      success: function(res) {
        console.log(res)
        that.setData({
          z_name: res.data.data.z_name
        })
      }
    })
  }
})